﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="ReceivedProducts.aspx.cs" Inherits="MonkeyWarranty.Web.WarrantyTickets.ReceivedProducts" %>

<%@ Import Namespace="Resources" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <title>
        <%=GlobalStrings.ProcessReceivedProducts %></title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <p>
        <a href="List.aspx"><%=GlobalStrings.Ticket %></a>&nbsp;|&nbsp;
        <a href="WaitingWarranty.aspx"><%=GlobalStrings.WaitingWarranty %></a>&nbsp;|&nbsp;
        <a href="ProcessWarranty.aspx"><%=GlobalStrings.ProcessWarranty%></a>&nbsp;|&nbsp;
        <a href="ReceivedProducts.aspx" class="a-selected"><%=GlobalStrings.ProcessReceivedProducts %></a>&nbsp;|&nbsp;
        <a href="LendProducts.aspx"><%= GlobalStrings.ProcessLendProducts %></a>
    </p>
    <div class="filtering">
        <label><%=GlobalStrings.Name %>:<input type="text" name="name" id="name" /></label>
        <label><%=GlobalStrings.ProductCode %>:<input type="text" name="code" id="code" /></label>
        <label><%=GlobalStrings.TicketCode %>:<input type="text" name="ticketcode" id="ticketcode" /></label>
        <button type="submit" id="LoadRecordsButton"><%=GlobalStrings.LoadRecords %></button>
        <button type="submit" id="ResetButton"><%=GlobalStrings.Reset %></button>
    </div>
    <div id="ProductsTableContainer">
    </div>
    <p>
    </p>
    <div>
        <input id="btnSendToTechnical" type="button" value="<%= GlobalStrings.SendBackCustomer %>"
            class="submitButton" onclick=" javascript:SendBackCustomer(); " />
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            LoadProducts();
        });
    </script>
    <script type="text/javascript">
        function LoadProducts() {
            $('#ProductsTableContainer').jtable({
                paging: true,
                pageSize: 15,
                sorting: true,
                defaultSorting: 'Name ASC',
                actions: {
                    listAction: 'ReceivedProducts.aspx/ProductList'
                },
                fields: {
                    Id: {
                        key: true,
                        create: false,
                        edit: false,
                        list: false
                    },
                    Select: {
                        title: '<input type="checkbox" id="chkAllProducts" onclick="javascript:ToggleAll();" />',
                        width: '1%',
                        sorting: false,
                        display: function (data) {
                            return '<input name="products" value="' + data.record.Id + '" type="checkbox" onclick="javascript:SelectProduct(' + data.record.Id + ');" />';
                        }
                    },
                    //WarrantyProductState: { title: '<%= GlobalStrings.Status %>', sorting: false },
                    TicketCode: { title: '<%= GlobalStrings.TicketCode %>', sorting: false },
                    ReceivedFromName: { title: '<%= GlobalStrings.ReceivedFrom %>', sorting: false },
                    DealerEmployeeName: { title: '<%= GlobalStrings.ReceivedFromName %>', width: '20%', sorting: false },
                    CustomerName: { title: '<%= GlobalStrings.Customers %>', sorting: false },
                    Phone: { title: '<%= GlobalStrings.Phone %>', sorting: false },
                    Code: {
                        title: '<%= GlobalStrings.ProductCode %>'
                    },
                    Name: {
                        title: '<%= GlobalStrings.ProductName %>'
                    },
                    Model: {
                        title: '<%= GlobalStrings.Model %>',
                        sorting: false
                    },
                    ManufacturerName: {
                        title: '<%= GlobalStrings.Manufacturers %>',
                        sorting: false
                    },
                    Serial: {
                        title: '<%= GlobalStrings.Serial %>',
                        sorting: false
                    },
                    Quantity: {
                        width: '5%',
                        title: '<%= GlobalStrings.Quantity %>',
                        sorting: false
                    },
                    WarrantyTypeName: {
                        title: '<%= GlobalStrings.WarrantyType %>',
                        sorting: false
                    }
                }
            });

            //Re-load records when user click 'load records' button.
            $('#LoadRecordsButton').click(function (e) {
                e.preventDefault();
                $('#ProductsTableContainer').jtable('load', {
                    name: $('#name').val(),
                    code: $('#code').val(),
                    ticketCode: $('#ticketcode').val()
                });
            });

            // Reset button
            $('#ResetButton').click(function (e) {
                e.preventDefault();
                $('#name').val('');
                $('#code').val('');
                $('#ticketcode').val('');
                $('#LoadRecordsButton').click();
            });

            //Load all records when page is first shown
            $('#LoadRecordsButton').click();
        }

        function SelectProduct(id) {
            var products = $('input[name=products]');
            for (var i = 0; i < products.length; i++) {
                if (products[i].checked == false) {
                    $('#chkAllProducts')[0].checked = false;
                    return;
                }
            }
            $('#chkAllProducts')[0].checked = true;
        }
    </script>
    <script type="text/javascript">
        function SendBackCustomer() {
            var products = $('input[name=products]');
            var productIds = new Array();
            for (var i = 0; i < products.length; i++) {
                if (products[i].checked)
                    productIds.push(products[i].value);
            }

            if (productIds.length == 0) {
                alert("<%=GlobalStrings.SelectProduct %>");
                return;
            }

            $.ajax({
                type: "POST",
                url: "ReceivedProducts.aspx/SendBackCustomer",
                data: JSON.stringify({ productIds: productIds }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    LoadProducts();
                },
                failure: function(errMsg) {
                    alert(errMsg);
                }
            });
        }

        function ToggleAll() {
            var chkAll = $('#chkAllProducts')[0];
            var products = $('input[name=products]');
            for (var i = 0; i < products.length; i++) {
                products[i].checked = chkAll.checked;
            }
        }
    </script>
</asp:Content>
